package com.example.quiz.controller;

import com.example.quiz.common.Result;
import com.example.quiz.model.User;
import com.example.quiz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String loginPage(@RequestParam(required = false) String redirect, HttpSession session) {
        if (redirect != null) {
            session.setAttribute("redirectUrl", redirect);
        }
        return "user/login";
    }

    @GetMapping("/register")
    public String registerPage() {
        return "user/register";
    }

    @PostMapping("/login")
    @ResponseBody
    public Result<String> login(@RequestParam String username,
            @RequestParam String password,
            HttpSession session) {
        User user = userService.login(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            // 根据用户角色返回不同的跳转路径
            String redirectUrl = user.isAdmin() ? "/admin" : "/";
            return Result.success(redirectUrl);
        }
        return Result.error("用户名或密码错误");
    }

    @PostMapping("/register")
    @ResponseBody
    public Result<Integer> register(@RequestBody User user) {
        System.out.println("Received registration request for user: " + user.getUsername());

        // 参数校验
        if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
            return Result.error("用户名不能为空");
        }
        if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
            return Result.error("密码不能为空");
        }

        int result = userService.register(user);
        System.out.println("Registration result: " + result);

        if (result > 0) {
            return Result.success(result);
        }
        return Result.error("注册失败，用户名已存在");
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/user/login";
    }

    @GetMapping("/info")
    @ResponseBody
    public Result<User> getUserInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("用户未登录");
    }

    @PutMapping("/password")
    @ResponseBody
    public Result<Integer> changePassword(@RequestParam Long userId,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {
        int result = userService.changePassword(userId, oldPassword, newPassword);
        if (result > 0) {
            return Result.success(result);
        }
        return Result.error("修改密码失败，原密码错误");
    }

    @GetMapping("/api/check-login")
    @ResponseBody
    public Result<User> checkLogin(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return Result.success(user);
        }
        return Result.error(401, "未登录");
    }
}
